﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class Employee : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        loadEmployeeIDs();
        dpdnEmployeeID_SelectedIndexChanged(null, null);
    }

    private void loadEmployeeIDs()
    {
        SqlConnection conn;
        string connectionString = ConfigurationManager.ConnectionStrings["GRNLdbaseConnection"].ConnectionString;
        conn = new SqlConnection(connectionString);

        SqlCommand commGetAllEmployeeID = new SqlCommand("sp_getAllEmployeeID", conn);
        commGetAllEmployeeID.CommandType = System.Data.CommandType.StoredProcedure;

        SqlDataReader reader;

        try
        {
            conn.Open();
            reader = commGetAllEmployeeID.ExecuteReader();
            dpdnEmployeeID.DataSource = reader;
            dpdnEmployeeID.DataValueField = "EmployeeID";
            dpdnEmployeeID.DataBind();
            reader.Close();
        }
        finally
        {
            conn.Close();
        }
    }
    protected void dpdnEmployeeID_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection conn;
        string connectionString = ConfigurationManager.ConnectionStrings["GRNLdbaseConnection"].ConnectionString;
        conn = new SqlConnection(connectionString);

        SqlCommand commGetEmployeeDetails = new SqlCommand("sp_SelectActiveEmployeeDetails", conn);
        commGetEmployeeDetails.CommandType = System.Data.CommandType.StoredProcedure;

        commGetEmployeeDetails.Parameters.Add("@EmployeeID", System.Data.SqlDbType.Int);
        commGetEmployeeDetails.Parameters["@EmployeeID"].Value = dpdnEmployeeID.SelectedValue;

        SqlDataReader reader;

        try
        {
            conn.Open();
            reader = commGetEmployeeDetails.ExecuteReader();
            while (reader.Read())
            {
                lblMsg.Text = "";
                txtFirstName.Text = reader["FirstName"]+"";
                txtLastName.Text = reader["LastName"] + "";
                txtEmail.Text = reader["EmailID"] + "";
                txtAddress.Text = reader["Address"] + "";
                txtPhoneNumber.Text = reader["ContactNumber"] + "";
            }
        }
        finally
        {
            conn.Close();
        }
    }
    protected void btnSaveChanges_Click(object sender, EventArgs e)
    {
        

        SqlConnection conn;
        string connectionString = ConfigurationManager.ConnectionStrings["GRNLdbaseConnection"].ConnectionString;
        conn = new SqlConnection(connectionString);

        SqlCommand commUpdateEmployeeDetails = new SqlCommand("sp_UpdateEmployeeDetails", conn);
        commUpdateEmployeeDetails.CommandType = System.Data.CommandType.StoredProcedure;

        commUpdateEmployeeDetails.Parameters.Add("@EmployeeID", System.Data.SqlDbType.Int);
        commUpdateEmployeeDetails.Parameters.Add("@FirstName", System.Data.SqlDbType.NVarChar, 50);
        commUpdateEmployeeDetails.Parameters.Add("@LastName", System.Data.SqlDbType.NVarChar, 50);
        commUpdateEmployeeDetails.Parameters.Add("@Address", System.Data.SqlDbType.NVarChar, 150);
        commUpdateEmployeeDetails.Parameters.Add("@ContactNumber", System.Data.SqlDbType.NChar,10);
        commUpdateEmployeeDetails.Parameters.Add("@EmailID", System.Data.SqlDbType.Text);


        commUpdateEmployeeDetails.Parameters["@EmployeeID"].Value = dpdnEmployeeID.Text;
        commUpdateEmployeeDetails.Parameters["@FirstName"].Value = txtFirstName.Text;
        commUpdateEmployeeDetails.Parameters["@LastName"].Value = txtLastName.Text;
        commUpdateEmployeeDetails.Parameters["@Address"].Value = txtAddress.Text;
        commUpdateEmployeeDetails.Parameters["@ContactNumber"].Value = txtPhoneNumber.Text;
        commUpdateEmployeeDetails.Parameters["@EmailID"].Value = txtEmail.Text;


        try
        {
            conn.Open();
            commUpdateEmployeeDetails.ExecuteNonQuery();

            dpdnEmployeeID_SelectedIndexChanged(null, null);
            lblMsg.Text = "Changes Saved Successfully!";
            dpdnEmployeeID.AutoPostBack = true;

        }
        catch (Exception err)
        {
            lblMsg.Text = "Updating Failed\n Error Message" + err.Message;
            conn.Close();
        }
        finally
        {
            conn.Close();
        }
        
    }
    protected void btnClearAll_Click(object sender, EventArgs e)
    {
        txtPhoneNumber.Text = "";
        txtAddress.Text = "";
        txtEmail.Text = "";
        txtFirstName.Text = "";
        txtLastName.Text = "";
        lblMsg.Text = "";
    }
    protected void btnEditRecord_Click(object sender, EventArgs e)
    {
        dpdnEmployeeID.AutoPostBack = false;
        txtAddress.ReadOnly = false;
        txtFirstName.ReadOnly = false;
        txtLastName.ReadOnly = false;
        txtPhoneNumber.ReadOnly = false;
        txtEmail.ReadOnly = false;
        btnSaveChanges.Enabled = true;
        btnEditRecord.Enabled = false;
    }
}